"""
@Author  : 康帅
@Time    : 2021/8/4 15:02
@Function: 力扣：3.无重复字符的最长子串(中等)
"""


class Solution(object):

    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        # 如果s="",需要计算第一个值,所以start从-1开始计算
        start = -1
        # max_data指的是最长的string
        max_data = 0
        # 使用dict暂时存储不重复字符串s作为值，s下标作为value，如果重复需要更新
        d = {}

        for i, value in enumerate(s):
            # s[i] in d
            if s[i] in d and d[s[i]] > start:
                start = d[s[i]]
                d[s[i]] = i
            # s[i] not in d
            else:
                d[s[i]] = i
                if i - start > max_data:
                    max_data = i - start

        return max_data


if __name__ == '__main__':
    data = Solution()
    print(data.lengthOfLongestSubstring(s="abbc"))
